﻿using ControlzEx.Standard;
using LoveNote.Application.Share;
using LoveNote.Application.Share.Login;
using LoveNote.Application.Share.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;
using TookKit.Mvvm.Events;

namespace LoveNote.Application.Main.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class MainViewModel
    {
        public ISession Session { get; }

        private ContentControl page = null;
        public ICommand LoadedCommand { get; }
        public ICommand NavigationToViewCommand { get; }
        public ICommand LogoutCommand { get; }

        public MainViewModel(ISession session)
        {
            Session = session;
            LoadedCommand = new RelayCommand<ContentControl>(OnLoadedCommand);
            NavigationToViewCommand = new RelayCommand<string>(OnNavigationToViewCommand);
            LogoutCommand = new RelayCommand(Logout);

            //修改客户事件
            EventBus.Default.Subscribe<ProcessMessage>((sender, message) =>
            {                
                this.page.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.ProcessView);//切换到ProcessView
            });

            //返回至CustomerView事件
            EventBus.Default.Subscribe<CustomerMessage>((sender, message) =>
            {
                this.page.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.CustomerView);//切换到CustomerView
            });
        }
        /// <summary>
        /// 登出
        /// </summary>
        private void Logout()
        {
            Session.CurrentUser = new Database.Entities.UserEntity();
            EventBus.Default.Publish(this, new LogoutMessage(null));
        }
        /// <summary>
        /// 导航页面
        /// </summary>
        /// <param name="viewName"></param>
        private void OnNavigationToViewCommand(string viewName)
        {
            this.page.Content = TookKit.Mvvm.Container.Default.GetService(viewName);
        }

        private void OnLoadedCommand(ContentControl page)
        {
            this.page = page;
            this.page.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.IndexView);
        }
    }
}
